﻿using General.Framework;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace General.Mvc.Filters
{
    public class UserCheckFilter: ActionFilterAttribute
    {
        public bool IsCheckUserLogin = true;
        //public TcUser LoginUser { get; set; }
        public override async void OnActionExecuting(ActionExecutingContext filterContext)
        {

            base.OnActionExecuting(filterContext);
            //if (Request.Method == "OPTIONS")
            //{
            //    filterContext.Result = Content("");
            //}

           

            ////登录状态下，执行刷新session在线缓存；不在线状态下，不进行缓存操作
            string userGuid = filterContext.HttpContext.Request.Headers["Auth"].ToString();

            var islogin = true;
            if (string.IsNullOrEmpty(userGuid))
            {
                islogin = false;
            }
            else
            {
                var userid = await RedisHelper.GetAsync("token" + userGuid);
                if (string.IsNullOrEmpty(userid))
                {
                    islogin = false;
                }
            }

            if (!islogin) //未登录
            {
                ContentResult content = new ContentResult() { StatusCode = 200, Content = JsonHelper.Serialize(new AjaxResult() { code = AjaxResultStateEnum.NoLogin, message = "请先登录！" }) };
                filterContext.Result = content;
            }



            //var userLogininfo = RedisHelper.Get(userGuid);


            //if (!string.IsNullOrEmpty(userLogininfo))
            //{
            //    //用户每执行操作，刷新session
            //    TcUser userInfo = JsonHelper.Deserialize<TcUser>(userLogininfo);

            //    LoginUser = userInfo;

            //    RedisHelper.Set(userGuid, JsonHelper.Serialize(userInfo), 60 * 60 * 24 * 10); //10天

            //    //滑动窗口机制
            //    //Common.Cache.CacheHelper.SetCache(userGuid, Common.SerializeHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));

            //    return;
            //}


        }
    }
}
